<?php

/**
 * role form base class.
 *
 * @method role getObject() Returns the current form's model object
 *
 * @package    e-DSL
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseroleForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                 => new sfWidgetFormInputHidden(),
      'description'        => new sfWidgetFormInputText(),
      'titre'              => new sfWidgetFormInputText(),
      'modifDemandes'      => new sfWidgetFormInputCheckbox(),
      'technicien'         => new sfWidgetFormInputCheckbox(),
      'interne'            => new sfWidgetFormInputCheckbox(),
      'reaffectation'      => new sfWidgetFormInputCheckbox(),
      'permissions_id'     => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('permissions'), 'add_empty' => true)),
      'activeStatus'       => new sfWidgetFormInputText(),
      'created_at'         => new sfWidgetFormDateTime(),
      'updated_at'         => new sfWidgetFormDateTime(),
      'reportdef_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'reportdef')),
      'filterdef_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'filterdef')),
      'documentation_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'documentation')),
    ));

    $this->setValidators(array(
      'id'                 => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'description'        => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'titre'              => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'modifDemandes'      => new sfValidatorBoolean(array('required' => false)),
      'technicien'         => new sfValidatorBoolean(array('required' => false)),
      'interne'            => new sfValidatorBoolean(array('required' => false)),
      'reaffectation'      => new sfValidatorBoolean(array('required' => false)),
      'permissions_id'     => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('permissions'), 'required' => false)),
      'activeStatus'       => new sfValidatorInteger(),
      'created_at'         => new sfValidatorDateTime(),
      'updated_at'         => new sfValidatorDateTime(),
      'reportdef_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'reportdef', 'required' => false)),
      'filterdef_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'filterdef', 'required' => false)),
      'documentation_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'documentation', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('role[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'role';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['reportdef_list']))
    {
      $this->setDefault('reportdef_list', $this->object->reportdef->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['filterdef_list']))
    {
      $this->setDefault('filterdef_list', $this->object->filterdef->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['documentation_list']))
    {
      $this->setDefault('documentation_list', $this->object->documentation->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->savereportdefList($con);
    $this->savefilterdefList($con);
    $this->savedocumentationList($con);

    parent::doSave($con);
  }

  public function savereportdefList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['reportdef_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->reportdef->getPrimaryKeys();
    $values = $this->getValue('reportdef_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('reportdef', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('reportdef', array_values($link));
    }
  }

  public function savefilterdefList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['filterdef_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->filterdef->getPrimaryKeys();
    $values = $this->getValue('filterdef_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('filterdef', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('filterdef', array_values($link));
    }
  }

  public function savedocumentationList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['documentation_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->documentation->getPrimaryKeys();
    $values = $this->getValue('documentation_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('documentation', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('documentation', array_values($link));
    }
  }

}
